//
//  TabbarManager.m
//  WuWuMap
//
//  Created by mymac on 2017/1/16.
//  Copyright © 2017年 Fujian first time iot technology investment co., LTD. All rights reserved.
//

#import "TabbarManager.h"
#import "MyViewController.h"
#import "DetailViewController.h"
#import "VipViewController.h"
#import "LogInViewController.h"
#import "ZWNavigationController.h"
#import "ZWTabBarViewController.h"
#import "TabBarBannerViewModel.h"
#import "TabBarBannerModel.h"
#import <SDWebImage/UIImage+MultiFormat.h>

@implementation TabbarManager



static NSString *homeKeyStr;
static NSString *rootsKeyStr;
static NSString *BusinessKeyStr;
static NSString *myKeyStr;

static TabbarManager* _instance = nil;

+(instancetype) shareInstance
{
    static dispatch_once_t onceToken ;
    dispatch_once(&onceToken, ^{
        _instance = [[super allocWithZone:NULL] init] ;
    }) ;
    
    return _instance ;
}

- (void)GoMainView
{
    
    
#pragma mark -----首页
  static  UIImage *homeImageSD;
   static  UIImage *rootsImageSD;
   static  UIImage *BusinessImageSD;
//   static  UIImage *myImageSD;
    
  
    [TabBarBannerViewModel tabbarPictureRequst:^(NSMutableArray *sender) {
    
                HQJLog(@"---%@",sender);
        if (sender.count != 0) {
                [[NSNotificationCenter defaultCenter]addObserverForName:NetworkStatus object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification * _Nonnull note) {
                    
                    if (MmberidStr) {
                        
                        if ( [note.userInfo[@"isNetWork"] isEqualToString: NoInternet]) {
                            [self iamgeStrOne:nil twoStrImage:nil threeStrImage:nil];
                            
                        } else {
                            
                            
                            homeImageSD =    [self OriginImage:[UIImage sd_imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGEURL,sender[0]]]]] scaleToSize:CGSizeMake(22, 22)];
                            
                            
                            
                            rootsImageSD = [self OriginImage:[UIImage sd_imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGEURL,sender[1]]]]] scaleToSize:CGSizeMake(22, 22)];
                            
                            BusinessImageSD = [self OriginImage:[UIImage sd_imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",IMAGEURL,sender[2]]]]] scaleToSize:CGSizeMake(22, 22)];
                            
                            
                            [self iamgeStrOne:homeImageSD twoStrImage:rootsImageSD threeStrImage:BusinessImageSD];
                            
                        }
                        
                        
                    }
                    
                    
                    }];
                    [ManagerEngine networkStatus];
            
            
            
                } else {
            
                    [self iamgeStrOne:nil twoStrImage:nil threeStrImage:nil];

            
                }
            
            
                    }];
    


    [self iamgeStrOne:nil twoStrImage:nil threeStrImage:nil];


//
}
-(void)dealloc {
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}


-(void) iamgeStrOne:(UIImage *)strOne twoStrImage:(UIImage *)strTwo threeStrImage:(UIImage *)strThree {
    
    UIImage *MyImage;
    UIImage *MyImageS;
    UIImage *mainImage;
    UIImage *mainImageS;
    UIImage *DetailImage;
    UIImage *DetailImageS;
   
   
   
    NSString * homeTabbarName = @"wode_normal";
    NSString * suyuanTabbarName = @"huiyuan_normal";
    NSString * shangjiaTabbarName = @"mingxi_normal";
    NSString * homeTabbarNames = @"wode_selected";
    NSString * suyuanTabbarNames = @"huiyuan_selected";
    NSString * shangjiaTabbarNames = @"mingxi_selected";
    

    
    MyViewController *MyVc = [[MyViewController alloc]init];
    VipViewController *VipVc = [[VipViewController alloc]init];
    DetailViewController *DetailVc = [[DetailViewController alloc]init];
    
    ZWNavigationController *MyNav = [[ZWNavigationController alloc]initWithRootViewController:MyVc];
    ZWNavigationController *VipNvc =[[ZWNavigationController alloc]initWithRootViewController:VipVc];
    ZWNavigationController *DetailNav = [[ZWNavigationController alloc]initWithRootViewController:DetailVc];
    [[UITabBar appearance]setTintColor:[ManagerEngine getColor:@"00ccb8"]];

    
    [MyNav.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -4)];
    [VipNvc.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -4)];
    [DetailNav.tabBarItem setTitlePositionAdjustment:UIOffsetMake(0, -4)];
    
    ZWTabBarViewController *tabbars = [[ZWTabBarViewController alloc]init];

    UITabBar *tabBar = tabbars.tabBar;
    
    for (UITabBarItem *item in tabBar.items) {
        
        item.imageInsets = UIEdgeInsetsMake(2, 2, 2,2);
        
    }
    
    
   // --如果一个图片的名字为空，则判断加载默认的
    if (strOne != nil) {
        
        tabbars.isAnimation = YES;  // -- 按钮动画
        
        MyImage  = strOne;
        MyImageS = [strOne imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        mainImage  = strTwo;
        mainImageS = [strTwo imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
        
        
        
        
        DetailImage  = strThree;
        DetailImageS = [strThree imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        MyVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我们" image:MyImageS selectedImage:MyImageS];
        VipVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"会员" image:mainImageS selectedImage:mainImageS];
        DetailVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"明细" image:DetailImageS selectedImage:DetailImageS];
        
        
    } else {
        
        tabbars.isAnimation = NO;

        MyImage  = [UIImage imageNamed:homeTabbarName];
        MyImageS  = [[UIImage imageNamed:homeTabbarNames]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        mainImage  = [UIImage imageNamed:suyuanTabbarName];
        mainImageS  = [[UIImage imageNamed:suyuanTabbarNames]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        
        DetailImage  = [UIImage imageNamed:shangjiaTabbarName];
        DetailImageS = [[UIImage imageNamed:shangjiaTabbarNames]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
        
        
 
        
        
        MyVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:MyImage selectedImage:MyImageS];
        VipVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"会员" image:mainImage selectedImage:mainImageS];
        DetailVc.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"明细" image:DetailImage selectedImage:DetailImageS];
    }

    
    
    tabbars.viewControllers = @[MyNav,VipNvc,DetailNav];
    
    
    [[UIApplication sharedApplication].delegate.window setRootViewController:tabbars];
    
    tabbars.view.backgroundColor = [UIColor clearColor];
    
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 420, 49)];
    
    [tabbars.tabBar insertSubview:backView atIndex:0];
    
    
    tabbars.tabBar.opaque = YES;
    
    


    
}



-(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    
    [image drawInRect:CGRectMake(0, 0, size.width, size.height)];
    
    UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return scaledImage;   //返回的就是已经改变的图片
}

@end
